package net.sasuke.firstapp;

import java.io.IOException;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class FirstServlet
 */
public final class FirstServlet extends HttpServlet {

   private static final long serialVersionUID = 1L;

   private ServletConfig config;

   /**
    * @see HttpServlet#HttpServlet()
    */
   public FirstServlet() {
      super();
   }

   /**
    * @see Servlet#init(ServletConfig)
    */
   public void init(ServletConfig config) throws ServletException {
      this.config = config;
   }

   /**
    * @see Servlet#destroy()
    */
   public void destroy() {
      System.out.println("Destroying servlet " + getServletName());
   }

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      handleRequest(request, response);
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
    *      response)
    */
   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      handleRequest(request, response);
   }

   private void handleRequest(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      HttpSession session = request.getSession();
      session.setAttribute("bean", new NameBean("OKI"));
      session.removeAttribute("bean");
      session.setAttribute("bean", null);
      session.setAttribute("baen", new NameBean("OH"));
      session.setAttribute("bean", new NameBean("NOKI"));

      // session.setMaxInactiveInterval(0);
      // session.setAttribute("A", "A");

      String displayName = request.getParameter("name");
      displayName = displayName == null ? config
            .getInitParameter("anonym-name") : displayName;
      response.getWriter().write("Welcome " + displayName);
      request.setAttribute("A", "FIRST-A");
      request.getRequestDispatcher("jsp/second.jsp").forward(request, response);
      request.setAttribute("A", "SECOND-A");
   }

   @Override
   protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      System.out.println("IN DELETE");
   }

   @Override
   protected void doHead(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      System.out.println("IN HEAD");
   }

   @Override
   protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      System.out.println("IN OPTIONS");
   }

   @Override
   protected void doPut(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      System.out.println("IN PUT");
   }

   @Override
   protected void doTrace(HttpServletRequest req, HttpServletResponse resp)
         throws ServletException, IOException {
      System.out.println("IN TRACE");
   }

}
